package com.wan37.logic.tradingshop.util;

import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.logic.tradingshop.domain.vo.TradingShopRecordVO;
import com.wan37.logic.tradingshop.domain.vo.TradingShopVO;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameTradingShop;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * @author : luoyong
 * @date : 2020-08-05 12:16
 **/
@Component
public class TradingShopRouteUtil {
    public void sendTradingShopResponse(int code, String userId, List<TradingShopVO> tradingShopVOList,
                                        List<TradingShopRecordVO> tradingShopRecordVOList){
        GameTradingShop.TradingShopResponse.Builder tsrb = GameTradingShop.TradingShopResponse.newBuilder();
        tsrb.setCode(code);
        if(tradingShopVOList != null){
            for (TradingShopVO tradingShopVO : tradingShopVOList){
                transferTradingShop(tsrb, tradingShopVO);
            }
        }
        if (tradingShopRecordVOList != null){
            for (TradingShopRecordVO tradingShopRecordVO : tradingShopRecordVOList){
                transferTradingShopRecord(tsrb, tradingShopRecordVO);
            }
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.TRADINGSHOPRESPONSE, tsrb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendTradingShopResponse(int code, String string, String userId){
        GameTradingShop.TradingShopResponse.Builder tsrb = GameTradingShop.TradingShopResponse.newBuilder();
        tsrb.setCode(code);
        if (string != null){
            tsrb.setString(string);
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.TRADINGSHOPRESPONSE, tsrb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendTradingShopResponse(int code, String string, String userId, TradingShopVO tradingShopVO){
        GameTradingShop.TradingShopResponse.Builder tsrb = GameTradingShop.TradingShopResponse.newBuilder();
        tsrb.setCode(code);
        if (string != null){
            tsrb.setString(string);
        }
        transferTradingShop(tsrb, tradingShopVO);
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.TRADINGSHOPRESPONSE, tsrb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    private void transferTradingShop(GameTradingShop.TradingShopResponse.Builder tsrb, TradingShopVO tradingShopVO){
        GameTradingShop.TradingShop.Builder tsb = GameTradingShop.TradingShop.newBuilder();
        tsb.setTradingShopId(tradingShopVO.getTradingShopId());
        tsb.setTradingShopType(tradingShopVO.getTradingShopType());
        tsb.setUserId(tradingShopVO.getUserId());
        tsb.setType(tradingShopVO.getType());
        tsb.setBackpackId(tradingShopVO.getBackpackId());
        tsb.setLevel(tradingShopVO.getLevel());
        tsb.setCount(tradingShopVO.getCount());
        tsb.setGold(tradingShopVO.getGold());
        tsb.setIngot(tradingShopVO.getIngot());
        tsb.setCreateTime(tradingShopVO.getCreateTime());
        tsrb.addTradingShop(tsb.build());
    }

    public void transferTradingShopRecord(GameTradingShop.TradingShopResponse.Builder tsrb, TradingShopRecordVO tradingShopRecordVO){
        GameTradingShop.TradingShopRecord.Builder tsr = GameTradingShop.TradingShopRecord.newBuilder();
        tsr.setRecordId(tradingShopRecordVO.getRecordId());
        tsr.setTradingShopId(tradingShopRecordVO.getTradingShopId());
        tsr.setIngot(tradingShopRecordVO.getIngot());
        tsr.setUserId(tradingShopRecordVO.getUserId());
        tsr.setCreateTime(tradingShopRecordVO.getCreateTime());
        tsr.setBuyId(tradingShopRecordVO.getBuyId());
        tsr.setBackpackId(tradingShopRecordVO.getBackpackId());
        tsrb.addTradingShopRecord(tsr.build());
    }
}
